{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 183,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"UTF-8\""
      ]
     },
     "execution_count": 183,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### pry-docの読み込み\n",
    "#require \"/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-doc-0.10.0/lib/pry-doc\"\n",
    "require \"/Users/ftakao2007/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-doc-0.10.0/lib/pry-doc\"\n",
    "### エンコーディングをUTF-8に設定\n",
    "Encoding.default_external = \"UTF-8\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Ruby技術者認定試験\n",
    "http://www.ruby.or.jp/ja/certification/examination/\n",
    "\n",
    "* 試験日時 : 2017/06/20 11:30\n",
    "* 試験時間 : 90分\n",
    "* 問題数 : 50門\n",
    "* 合格ライン : 75%\n",
    "* 対象バージョン : 2.1.X\n",
    "* 出題範囲\n",
    "    * 文法\n",
    "        * [済]コメント\n",
    "        * リテラル（数値、真偽値、文字列、文字、配列、ハッシュ等）\n",
    "        * 変数/定数とスコープ\n",
    "        * 演算子\n",
    "        * 条件分岐\n",
    "        * ループ\n",
    "        * 例外処理\n",
    "        * メソッド呼び出し\n",
    "        * ブロック\n",
    "        * メソッド定義\n",
    "        * クラス定義\n",
    "        * モジュール定義\n",
    "        * 多言語対応\n",
    "    * 組み込みライブラリ\n",
    "        * よく使用されるクラス、モジュール\n",
    "            * Object\n",
    "            * 数値クラス\n",
    "            * String\n",
    "            * Array\n",
    "            * Hash\n",
    "            * Kernel\n",
    "            * Enumerable\n",
    "            * Comparable\n",
    "            * etc...\n",
    "    * オブジェクト指向\n",
    "        * ポリモルフィズム\n",
    "        * 継承\n",
    "        * mix-in"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Ruby技術者認定試験 模擬問題活用方法\n",
    "http://www.ruby.or.jp/ja/certification/examination/prep/prep_test.html\n",
    "\n",
    "# Ruby技術者認定試験 version2.1 Silver模擬問題集\n",
    "http://www.ruby.or.jp/ja/certification/examination/index.data/exam_prep_jp.pdf\n",
    "\n",
    "※ コード実行欄はコメントアウトを外して実行すると答えが出る"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題1. 選択肢の中からRubyの予約語ではないものを2つ選択してください。\n",
    "\n",
    "1. super\n",
    "1. begin\n",
    "1. try\n",
    "1. goto\n",
    "\n",
    "〇 〇\n",
    "\n",
    "<!-- 3,4 -->"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "### 予約語の場合変数として使えない\n",
    "\n",
    "#super=1\n",
    "#p super\n",
    "#begin=2\n",
    "#p begin\n",
    "#try=3\n",
    "#p try\n",
    "#goto=4\n",
    "#p goto"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題2. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "* 「`*`」が付いた引数は可変長引数(rest引数)\n",
    "\n",
    "× 〇 〇 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "def foo (a, *b)\n",
    "#p a\n",
    "#p b\n",
    "end\n",
    "foo(1,2,3,4)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題3. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#puts({\"members\" => 193, \"year\" => 2014}.size)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題4. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#t = Time.now + (60*60*24)\n",
    "#p Time.now\n",
    "#p t"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題5. ローカル変数の名前として正しいものをすべて選択してください。\n",
    "\n",
    "1. `_365`\n",
    "1. z\n",
    "1. 7years\n",
    "1. break\n",
    "1. latitude\n",
    "\n",
    "〇 ☓ 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "SyntaxError: unexpected '=', expecting end-of-input\n",
      "break=4\n",
      "      ^\n"
     ]
    }
   ],
   "source": [
    "### ローカル変数に使えないものは代入できない\n",
    "# _365=1\n",
    "# p _365\n",
    "# z=2\n",
    "# p z\n",
    "# 7years=3\n",
    "# p 7years\n",
    "# break=4\n",
    "# p break\n",
    "# atitude=5\n",
    "# p atitude"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題6. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "* 「..」と「...」の違い\n",
    "    * 一回目は意味を逆に認識してしまっていた\n",
    "\n",
    "× 〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# (5..8).each_with_index do |val,i|\n",
    "# puts \"#{i} #{val}\"\n",
    "# end\n",
    "\n",
    "# (5...8).each_with_index do |val,i|\n",
    "# puts \"#{i} #{val}\"\n",
    "# end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題7. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#p 100.downto(90).select{|x| x%2==0}\n",
    "### 同じ挙動のメソッド\n",
    "#p 100.downto(90).hoge{|x| x%2==0}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題8. 以下のコードを実行した時の出力に対して__（１）__に入る適切な記述を２つ選択してください。\n",
    "\n",
    "```\n",
    "p [1,1,2,3,5,8].__(1)__｛|x| x*2}\n",
    "[出力]\n",
    "[2,2,4,6,10,16]\n",
    "```\n",
    "\n",
    "〇 ☓ 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#p [1,1,2,3,5,8].hoge{|x| x*2}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題9. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#puts \"Ruby on Rails\".delete(\"Rails\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題10.ヒアドキュメントを使用して文字列を作成しようとしたが実行時エラーになった。正しく動かすための修正点を2パターン選択してください。\n",
    "\n",
    "```\n",
    "doc = <<EOF\n",
    " The quick brown fox\n",
    " jumps over the lazy dog\n",
    " EOF\n",
    "```\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "(pry):135: can't find string \"EOF\" anywhere before EOF\n(pry):132: syntax error, unexpected end-of-input, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END",
     "output_type": "error",
     "traceback": [
      "\u001b[31mSyntaxError\u001b[0m: (pry):135: can't find string \"EOF\" anywhere before EOF\n(pry):132: syntax error, unexpected end-of-input, expecting tSTRING_CONTENT or tSTRING_DBEG or tSTRING_DVAR or tSTRING_END",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/backend.rb:73:in `eval'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/backend.rb:12:in `eval'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:87:in `execute_request'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:47:in `dispatch'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:37:in `run'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/command.rb:70:in `run_kernel'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/command.rb:34:in `run'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/bin/iruby:5:in `<top (required)>'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/bin/iruby:23:in `load'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/bin/iruby:23:in `<main>'\u001b[0m"
     ]
    }
   ],
   "source": [
    "doc = <<EOF\n",
    " The quick brown fox\n",
    " jumps over the lazy dog\n",
    " EOF"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題11.以下の__(1)__に入る適切な記述を１つ入れてください。\n",
    "\n",
    "```\n",
    "h = {1=>2, 3=>4}\n",
    "h.__(1)__\n",
    "p h #=>{}\n",
    "ary = [1, 2]\n",
    "ary.__(1)__\n",
    "p ary #=>[]\n",
    "```\n",
    "\n",
    "〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{}\n",
      "[1, 2]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 2]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h = {1=>2, 3=>4}\n",
    "#h.hoge\n",
    "p h\n",
    "\n",
    "ary = [1,2]\n",
    "#ary.hoge\n",
    "p ary"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題12.以下の__(1)__に入る適切な記述を2つ入れてください。\n",
    "\n",
    "```\n",
    "a = [1,2,3,4,5]\n",
    "p __(1)__ #=>[1,2,3]\n",
    "```\n",
    "\n",
    "× × × 〇\n",
    "\n",
    "* <font color=\"red\">問題6と関連して覚える</font>\n",
    "* 配列の場合はindexの指定であることに注意"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,3,4,5]\n",
    "\n",
    "#p a[X,Y]\n",
    "#p a[-X,Y]\n",
    "#p a.slice(X,Y)\n",
    "#p a.slice(X..Y)\n",
    "#p a.slice(X...Y)\n",
    "\n",
    "p a[-5,3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題13. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#str = \"RubyAssociation\".chomp\n",
    "\n",
    "\n",
    "#\"RubyAssociation\".chop\n",
    "#\"RubyAssociation\".chop.chop"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題14. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# str = \"RubyAssociation\\r\\n\".chop\n",
    "# p str\n",
    "\n",
    "#\"RubyAssociation\\r\\n\".chomp.chomp"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題15. 以下のコードにおける4行目io.rewindについて正しい説明を１つ選択してください。\n",
    "\n",
    "```\n",
    "File.open(\"foo.txt\",\"r\") do |io|\n",
    "  puts io.gets\n",
    "  puts io.read\n",
    "  io.rewind\n",
    "  p lines = io.readlines\n",
    "end\n",
    "```\n",
    "\n",
    "1. データを全て読み込んで、その各行を要素としてもつ配列を返す\n",
    "1. ファイルポインターをファイルの先頭に移動する\n",
    "1. IO から1バイトを読み込み整数として返す\n",
    "1. レシーバと同じ IO を参照する新しい IO オブジェクトを返す\n",
    "\n",
    "× 〇 〇\n",
    "\n",
    "* IOクラスでは、IO#rewindの他に、 seek、gets、read、readlineなどのインスタンスメソッドがある\n",
    "* seek : https://docs.ruby-lang.org/ja/latest/method/IO/i/seek.html"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\"foo\\n\", \"bar\\n\", \"baz\\n\"]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[\"foo\\n\", \"bar\\n\", \"baz\\n\"]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "### foo.txt\n",
    "### foo\n",
    "### bar\n",
    "### baz\n",
    "\n",
    "File.open(\"foo.txt\",\"r\") do |io|\n",
    "  ### io.getsの挙動\n",
    "  #puts io.gets\n",
    "  #puts io.gets\n",
    "  #puts io.gets\n",
    "  #puts io.gets\n",
    "\n",
    "  ### io.readの挙動\n",
    "  #puts io.read\n",
    "  #puts io.read\n",
    "  \n",
    "  ### io.rewindの挙動\n",
    "  #puts io.gets\n",
    "  #puts io.gets\n",
    "  #io.rewind\n",
    "  #puts io.gets\n",
    "  \n",
    "  ### io.seekの挙動\n",
    "#   puts io.read\n",
    "#   puts \"### seek1\"\n",
    "#   io.seek(-11,IO::SEEK_CUR)\n",
    "#   puts io.read\n",
    "#   puts \"### seek2\"\n",
    "#   io.seek(-6,IO::SEEK_CUR)\n",
    "#   puts io.read\n",
    "#   puts \"### seek3\"\n",
    "#   io.seek(-3,IO::SEEK_CUR)\n",
    "#   puts io.read\n",
    "  \n",
    "  #io.readlinesの挙動\n",
    "#   p lines = io.readlines\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題16. 以下のコードを実行した時の出力に対して__（１）__に入る適切な記述は何でしょうか。\n",
    "\n",
    "```\n",
    "mozart = [\"Symphony\",\"Piano Concerto\", \"Violin Concerto\",\"Horn Concerto\",\"Violin Sonata\"]\n",
    "listend = [\"Symphony\",\"Violin Concerto\",\"Horn Concerto\"]\n",
    "p mozart__(1)__listend\n",
    "[出力]\n",
    "[\"Piano Conerto\",\"Violin Sonata\"]\n",
    "```\n",
    "\n",
    "× 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[\"Symphony\", \"Violin Concerto\", \"Horn Concerto\"]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mozart = [\"Symphony\",\"Piano Concerto\", \"Violin Concerto\",\"Horn Concerto\",\"Violin Sonata\"]\n",
    "listend = [\"Symphony\",\"Violin Concerto\",\"Horn Concerto\"]\n",
    "#p mozart+listend\n",
    "#p mozart-listend\n",
    "#p mozart&listend\n",
    "#p mozart|listend"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題17. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 4, 6]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "odd = [1,3,5]\n",
    "even = [2,4,6]\n",
    "# num = odd + even\n",
    "# p num.sort"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題18. 下記の正規表現にマッチする文字列を2つ選択してください。\n",
    "\n",
    "```\n",
    "/^[hc].*o$/i\n",
    "```\n",
    "\n",
    "1. Hello\n",
    "1. holland\n",
    "1. Cello\n",
    "1. h35L320\n",
    "\n",
    "〇 〇 〇\n",
    "\n",
    "* 正規表現のオプションなど確認する"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n"
     ]
    }
   ],
   "source": [
    "#puts \"Hello\".match(/^[hc].*o$/i)\n",
    "# puts \"holland\".match(/^[hc].*o$/i)\n",
    "# puts \"Cello\".match(/^[hc].*o$/i)\n",
    "# puts \"h35L320\".match(/^[hc].*o$/i)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題19. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# Greeting = \"Hello Ruby\"\n",
    "# Greeting = \"Hi Ruby\"\n",
    "# p Greeting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題20. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× 〇 × 〇 〇\n",
    "\n",
    "* File.joinのデフォルトの挙動は\n",
    "    * <font color=\"red\">Fileクラスの場合のセパレータ</font>は何になるか\n",
    "        * 頭にはつかない\n",
    "    * Arrayクラスの場合のデフォルトのセパレータは？\n",
    "    * Hashクラスにはjoinは無し"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#p File.join(\"ruby\", \"exam\",\"silver\")\n",
    "#p [1,2,3].join"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題21. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       ":initialize"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Surface\n",
    "  attr_reader :s\n",
    "  def initialize(x,y)\n",
    "    @s = x * y\n",
    "  end\n",
    "end\n",
    "\n",
    "class Volume < Surface\n",
    "  attr_reader :v\n",
    "  def initialize(x,y,z)\n",
    "    super(x,y)\n",
    "    @v = x * y * z\n",
    "  end\n",
    "end\n",
    "\n",
    "# a = Volume.new(2,5,5)\n",
    "# puts \"#{a.v},#{a.s}\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題22. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× 〇 〇 〇\n",
    "\n",
    "* <font color=\"red\">破壊的メソッドと非破壊的なメソッドを認識する</font>\n",
    "* <font color=\"red\">破壊的メソッドで文字列を変更した後の変数の中身はどうなるか</font>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# string = \"test code\"\n",
    "# p string.slice(0,4)\n",
    "# p string\n",
    "\n",
    "# string = \"test code\"\n",
    "# p string.slice!(0,4)\n",
    "# p string"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題23. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× 〇 〇\n",
    "\n",
    "* Ruby 2.0以降では、デフォルトのスクリプトエンコーディングは何か"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 277,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# puts \"ruby\".encoding\n",
    "# puts \"ruby\".encoding.name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題24. ハッシュ内にキーが存在するかどうか調べるメソッドの使いかたとして、誤っているものを1つ選択してください。\n",
    "\n",
    "```\n",
    "cc = {\"nz\"=>\"New Zealand\", \"ru\"=>\"Russia\", \"ar\"=>\"Argentina\"}\n",
    "```\n",
    "\n",
    "1. p cc.has_key?('nz')\n",
    "1. p cc.contain?('nz')\n",
    "1. p cc.key?('nz')\n",
    "1. p cc.include?('nz')\n",
    "1. p cc.member?('nz')\n",
    "\n",
    "〇 ☓ 〇 × ○"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{\"nz\"=>\"New Zealand\", \"ru\"=>\"Russia\", \"ar\"=>\"Argentina\"}"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "cc = {\"nz\"=>\"New Zealand\", \"ru\"=>\"Russia\", \"ar\"=>\"Argentina\"}\n",
    "\n",
    "# p cc.has_key?('nz')\n",
    "# p cc.contain?('nz')\n",
    "# p cc.key?('nz')\n",
    "# p cc.include?('nz')\n",
    "# p cc.member?('nz')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{\"ru\"=>\"Russia\"}\n",
      "\n",
      "[\"New Zealand\"]\n"
     ]
    }
   ],
   "source": [
    "cc = {\"nz\"=>\"New Zealand\", \"ru\"=>\"Russia\"}\n",
    "cc.delete(\"nz\")\n",
    "puts cc\n",
    "puts\n",
    "\n",
    "cc = [\"nz\", \"New Zealand\"]\n",
    "cc.delete(\"nz\")\n",
    "puts cc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題25. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 283,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "String\n",
      "Object\n",
      "Object\n",
      "Foo\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Foo"
      ]
     },
     "execution_count": 283,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# p \"Hello there\".class\n",
    "# p String.superclass\n",
    "# class Foo; end\n",
    "# class Bar < Foo; end\n",
    "# p Foo.superclass\n",
    "# p Bar.superclass "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題26. 以下のコードを実行して文字列sの単語毎の出現回数を出力させたい。`__(1)__,__(2)__`に入る最適な組み合わせを１つ選択してください。\n",
    "\n",
    "```\n",
    "s = \"To be or not to be, that is the question.\"\n",
    "hash = Hash.new(0)\n",
    "s.__(1)__(__(2)__) {|i| hash[i] += 1}\n",
    "p hash[\"be\"] #=>2\n",
    "```\n",
    "\n",
    "1. (1)match (2)/\\w+/\n",
    "1. (1)sub (2)/\\w+/\n",
    "1. (1)scan (2)/\\w+/\n",
    "1. (1)search (2)/\\w+/\n",
    "\n",
    "× 〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# s = \"To be or not to be, that is the question.\"\n",
    "# hash = Hash.new(0)\n",
    "# s.hoge(/\\w+/) {|i| hash[i] += 1}\n",
    "# p hash[\"be\"] #=>2\n",
    "# p hash"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題27. 組み込みライブラリ、Integer#chr(encoding)についての説明として正しいものはどれか、2つ選択してください。\n",
    "\n",
    "1. 引数を指定しなかった場合はエラーが発生する。\n",
    "1. 指定されたエンコーディングでselfを正しく解釈できない場合はnilが返される。\n",
    "1. 引数で与えられたencodingにおいて、selfを文字コードと見なし、それに対応する一文字からなる文字列を返す。\n",
    "1. 指定されたエンコーディングでselfを正しく解釈できない場合はエラーが発生する。\n",
    "\n",
    "× 〇 × 〇\n",
    "\n",
    "<!--\n",
    "3,4\n",
    "-->"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#12354.chr(Encoding::UTF_8)\n",
    "#3478419.chr(Encoding::UTF_8)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#show-doc Integer#chr"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題28. 以下のプログラムがerrorと出力する場合__(1)__,__(2)__に入る最適な組み合わせを１つ選択してください。\n",
    "\n",
    "```\n",
    "s = \"foo\"\n",
    "___(1)___\n",
    " s[4] = ?b\n",
    "___(2)___\n",
    " puts \"error\"\n",
    "end\n",
    "```\n",
    "\n",
    "1. `___(1)___`は「try」、`___(2)___`は「catch NameError」である\n",
    "1. `___(1)___`は「try」、`___(2)___`は「catch IndexError」である\n",
    "1. `___(1)___`は「begin」、`___(2)___`は「rescue NameError」である\n",
    "1. `___(1)___`は「begin」、`___(2)___`は「rescue IndexError」である\n",
    "\n",
    "〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "index 4 out of string",
     "output_type": "error",
     "traceback": [
      "\u001b[31mIndexError\u001b[0m: index 4 out of string",
      "\u001b[37m(pry):255:in `[]='\u001b[0m",
      "\u001b[37m(pry):255:in `<main>'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/backend.rb:66:in `eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/backend.rb:12:in `eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:87:in `execute_request'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:47:in `dispatch'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:37:in `run'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/command.rb:70:in `run_kernel'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/command.rb:34:in `run'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/bin/iruby:5:in `<top (required)>'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/bin/iruby:22:in `load'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/bin/iruby:22:in `<main>'\u001b[0m"
     ]
    }
   ],
   "source": [
    "s = \"foo\"\n",
    "\n",
    "# s[3] = ?b\n",
    "# s[4] = ?b\n",
    "# p s\n",
    "\n",
    "# begin\n",
    "#  s[4] = ?b\n",
    "# rescue NameError\n",
    "#  puts \"error\"\n",
    "# end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題29. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× 〇 〇 〇 ×"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1]\n",
      "\"hoge\"\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\"hoge\""
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# ary = []\n",
    "# ary << 1 && false\n",
    "# true || ary << 2\n",
    "# false && ary << 3\n",
    "# false || ary << 4\n",
    "# p ary\n",
    "\n",
    "# ary2 =[]\n",
    "# ary2 << 1 && a = \"hoge\"\n",
    "# p ary2\n",
    "# p a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "* 論理演算子の評価方法\n",
    "    * <font color=\"red\">&&演算子においては左辺が真と評価されたときのみ右辺も評価される</font>\n",
    "    * <font color=\"red\">||演算子においては左辺が偽と評価されたときのみ右辺も評価される</font>\n",
    "* 今回の問題とif文での挙動について\n",
    "    * if文\n",
    "        * ||演算子は左辺がtrueの場合は以降は評価せずtrueを返す\n",
    "        * `if true || false` ※ trueで評価が終わる\n",
    "    * 今回の問題\n",
    "        * ||演算子は左辺がtrueの場合は以降は評価しない\n",
    "        * `true || ary << 2` ※ aryに2は入らない"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題30. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#p \"foo\" * 2 **2"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題31. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× 〇 〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# foo = [1,2,3]\n",
    "# bar = foo\n",
    "# baz = foo.dup\n",
    "# p foo\n",
    "# p bar\n",
    "# p baz\n",
    "# p foo.object_id\n",
    "# p bar.object_id\n",
    "# p baz.object_id\n",
    "# bar[3] = 4\n",
    "# p foo\n",
    "# p bar\n",
    "# p baz"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "元の文字列\n",
      "abc\n",
      "\n",
      "update1でstrを上書き。メソッドの外のstrはそのまま\n",
      "def\n",
      "abc\n",
      "\n",
      "update2でstr[1]を上書き。メソッドの外のstrは更新される\n",
      "g\n",
      "agc\n",
      "\n",
      "複製して利用すると更新されない\n",
      "g\n",
      "abc\n"
     ]
    }
   ],
   "source": [
    "### 変数\n",
    "def update1(var)\n",
    "  var = \"def\"\n",
    "end\n",
    "\n",
    "def update2(var)\n",
    "  var[1] = \"g\"\n",
    "end\n",
    "\n",
    "puts \"元の文字列\"\n",
    "str = \"abc\"\n",
    "puts str\n",
    "puts\n",
    "\n",
    "puts \"update1でstrを上書き。メソッドの外のstrはそのまま\"\n",
    "puts update1(str)\n",
    "puts str\n",
    "puts\n",
    "\n",
    "puts \"update2でstr[1]を上書き。メソッドの外のstrは更新される\"\n",
    "puts update2(str)\n",
    "puts str\n",
    "puts\n",
    "\n",
    "puts \"複製して利用すると更新されない\"\n",
    "str = \"abc\"\n",
    "def update3(var)\n",
    "  var = var.dup\n",
    "  var[2] = \"g\"\n",
    "end\n",
    "\n",
    "puts update3(str)\n",
    "puts str"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "元の配列\n",
      "[1, 2, 3]\n",
      "\n",
      "update1でaryを上書き。メソッドの外のaryはそのまま\n",
      "[\"a\", \"b\", \"c\"]\n",
      "[1, 2, 3]\n",
      "\n",
      "update2でary[1]を上書き。メソッドの外のaryは更新される\n",
      "g\n",
      "[1, \"g\", 3]\n",
      "\n",
      "複製して利用すると更新されない\n",
      "g\n",
      "[1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "### 配列\n",
    "def update1(var)\n",
    "  var = [\"a\",\"b\",\"c\"]\n",
    "end\n",
    "\n",
    "def update2(var)\n",
    "  var[1] = \"g\"\n",
    "end\n",
    "\n",
    "puts \"元の配列\"\n",
    "ary = [1,2,3]\n",
    "puts ary\n",
    "puts\n",
    "\n",
    "puts \"update1でaryを上書き。メソッドの外のaryはそのまま\"\n",
    "puts update1(ary)\n",
    "puts ary\n",
    "puts\n",
    "\n",
    "puts \"update2でary[1]を上書き。メソッドの外のaryは更新される\"\n",
    "puts update2(ary)\n",
    "puts ary\n",
    "puts\n",
    "\n",
    "puts \"複製して利用すると更新されない\"\n",
    "ary = [1,2,3]\n",
    "def update3(var)\n",
    "  var = var.dup\n",
    "  var[2] = \"g\"\n",
    "end\n",
    "\n",
    "puts update3(ary)\n",
    "puts ary"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "* 変数や配列が保持しているのは<font color=\"red\">オブジェクトへの参照</font>\n",
    "    * メソッドの引数に新しいオブジェクトを代入した場合は元のオブジェクトは変更されない\n",
    "    * メソッドに渡した引数の参照を変更する場合、元のオブジェクトが更新される\n",
    "        * dupでオブジェクトを複製してから更新すると、元のオブジェクトは更新されない"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題32. テキストファイルを読み込んだファイルオブジェクトから一行ずつ読み込み表示したい。目的に一致するIOクラスのメソッドを２つ選択してください。\n",
    "\n",
    "1. readlines\n",
    "1. read\n",
    "1. gets\n",
    "1. readline\n",
    "1. find\n",
    "\n",
    "× × 〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "Dir.mkdir(\"/tmp/tmp2\", 0755) unless Dir.exist?(\"/tmp/tmp2\")\n",
    "Dir.chdir(\"/tmp/tmp2\")\n",
    "File.open(\"foo.txt\",\"w\") do |io|\n",
    "  io.write(\"foo\\n\")\n",
    "  io.write(\"bar\\n\")\n",
    "  io.write(\"baz\")\n",
    "end\n",
    "\n",
    "File.open(\"foo.txt\",\"r\") do |io|\n",
    "#  puts io.readlines\n",
    "#  puts io.read\n",
    "#  puts io.gets\n",
    "#  puts io.readline\n",
    "#  p io.find\n",
    "end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "IO#getsとIO#readlineの違いはEOFに到達した時の振る舞いのみです。IO#getsはnil, IO#readlineはEOFErrorを返します。"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題33. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× 〇 〇 × ○"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# str = \"a,b,c,d\"\n",
    "# p str\n",
    "# p str.split(/,/, 2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題34. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# class Object\n",
    "#   def greeting\n",
    "#     print \"How are you?\\n\"\n",
    "#   end\n",
    "# end\n",
    "# [1,2].greeting"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "* 定義済みクラス(Object)にメソッドを追加している\n",
    "    * 追加したメソッドの挙動"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題35. 以下の正規表現とマッチするものを1つ選択してください。\n",
    "\n",
    "```\n",
    "/[0-9]{3}-[0-9]{4}/\n",
    "```\n",
    "\n",
    "1. 0x000000\n",
    "1. 0-93-0-94\n",
    "1. 93094\n",
    "1. 333-4421\n",
    "\n",
    "〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# p /[0-9]{3}-[0-9]{4}/ =~ \"0x000000\"\n",
    "# p /[0-9]{3}-[0-9]{4}/ =~ \"0-93-0-94\"\n",
    "# p /[0-9]{3}-[0-9]{4}/ =~ \"93094\"\n",
    "# p /[0-9]{3}-[0-9]{4}/ =~ \"333-4421\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題36. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#\"a\".upto(\"f\").to_a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題37. 以下のコードを実行した時にIOErrorが発生した。理由として考えられるものはどれか１つ選択してください。\n",
    "\n",
    "```\n",
    "File.open(\"foo.txt\") do |io|\n",
    " io.write(Time.now.strftime(\"%Y/%m/%d\"))\n",
    "end\n",
    "```\n",
    "\n",
    "1. 書き込みモードでファイルが開かれているため\n",
    "1. 読み込み可能なfoo.txtが存在しなかったため。\n",
    "1. 読み込みモードでファイルが開かれているため\n",
    "1. 読み書き両用モードでファイルが開かれているため\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# File.open(\"foo.txt\") do |io|\n",
    "#  io.write(Time.now.strftime(\"%Y/%m/%d\"))\n",
    "# end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題38. 次のメソッドでDirクラスのクラスメソッドではないものをすべて選択してください。(2つ選択)\n",
    "\n",
    "1. Dir.rmdir\n",
    "1. Dir.basename\n",
    "1. Dir.pwd\n",
    "1. Dir.extname\n",
    "1. Dir.getwd\n",
    "\n",
    "× 〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#Dir.rmdir\n",
    "#Dir.basename\n",
    "#Dir.pwd\n",
    "#Dir.extname\n",
    "#Dir.getwd"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題39. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "* 範囲指定の書き方\n",
    "    * どう書けば範囲指定となるか、ならないか\n",
    "\n",
    "× × 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# puts \"012345678-9\".delete(\"0-5\")\n",
    "# puts \"012345678-9\".delete(\"0-58\")\n",
    "# puts \"012345678-9\".delete(\"0-58-\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題40. 下のコードを実行した時の出力に対して`__(1)__,__(2)__`に入る最適な組み合わせを１つ選択してください。\n",
    "\n",
    "```\n",
    "p \"100,200,300,400,500\".___(1)___.___(2)___\n",
    "[出力]\n",
    "\"100\\n200\\n300\\n400\\n500\"\n",
    "```\n",
    "\n",
    "1. (1)split(\",\")(2)join(\"\\n\")\n",
    "1. (1)to_a(\",\")(2)join(\"\\n\")\n",
    "1. (1)split(\",\")(2)concat(\"\\n\")\n",
    "1. (1)concat(\",\")(2)join(\"\\n\")\n",
    "\n",
    "〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#p \"100,200,300,400,500\".split(\",\").join(\"\\n\")\n",
    "#p \"100,200,300,400,500\".to_a(\",\").join(\"\\n\")\n",
    "#p \"100,200,300,400,500\".split(\",\").concat(\"\\n\")\n",
    "#p \"100,200,300,400,500\".concat(\",\").join(\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m string.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m String\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m concat(arg1)\n",
      "\u001b[1mNumber of lines:\u001b[0m 7\n",
      "\n",
      "Append---Concatenates the given object to \u001b[1mstr\u001b[0m. If the object is a\n",
      "\u001b[1;34;4mInteger\u001b[0m, it is considered as a codepoint, and is converted\n",
      "to a character before concatenation.\n",
      "\n",
      "   a = \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mhello \u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m\n",
      "   a << \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mworld\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m   \u001b[1;34m#=> \"hello world\"\u001b[0m\n",
      "   a.concat(\u001b[1;34m33\u001b[0m)   \u001b[1;34m#=> \"hello world!\"\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc String#concat"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m hash.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Hash\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m to_a()\n",
      "\u001b[1mNumber of lines:\u001b[0m 5\n",
      "\n",
      "Converts \u001b[1mhsh\u001b[0m to a nested array of [ \u001b[1mkey,\n",
      "value\u001b[0m ] arrays.\n",
      "\n",
      "   h = { \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mc\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m => \u001b[1;34m300\u001b[0m, \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31ma\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m => \u001b[1;34m100\u001b[0m, \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31md\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m => \u001b[1;34m400\u001b[0m, \u001b[31m\u001b[1;31m\"\u001b[0m\u001b[31mc\u001b[1;31m\"\u001b[0m\u001b[31m\u001b[0m => \u001b[1;34m300\u001b[0m  }\n",
      "   h.to_a   \u001b[1;34m#=> [[\"c\", 300], [\"a\", 100], [\"d\", 400]]\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc Hash#to_a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hoge fuga\n",
      "hoge fuga\n",
      "\n",
      "hoge fuga haga \n",
      "hoge fuga haga \n",
      "hoge fuga haga A\n"
     ]
    }
   ],
   "source": [
    "str = \"hoge\"\n",
    "puts str.concat(\" fuga\")\n",
    "puts str\n",
    "puts\n",
    "\n",
    "puts str << \" haga \"\n",
    "puts str\n",
    "\n",
    "puts str.concat(65)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題41. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "true\n",
      "false\n"
     ]
    }
   ],
   "source": [
    "# a = \"foo\"\n",
    "# b = \"foo\"\n",
    "# puts a.eql?(b)\n",
    "# puts a.equal?(b)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題42. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 × 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "89\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "89"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numbers = [3,89,40,39,29,10,50,59,69]\n",
    "# num = numbers.inject do |i,j|\n",
    "#  i > j ? i : j\n",
    "# end\n",
    "# p num"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15\n",
      "120\n",
      "5\n"
     ]
    }
   ],
   "source": [
    "puts [1,2,3,4,5].inject{|i, j| i + j}\n",
    "puts [1,2,3,4,5].inject{|i, j| i * j}\n",
    "puts [1,2,3,4,5].inject{|i, j| i > j ? i : j}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\u001b[1mFrom:\u001b[0m enum.c (C Method):\n",
      "\u001b[1mOwner:\u001b[0m Enumerable\n",
      "\u001b[1mVisibility:\u001b[0m public\n",
      "\u001b[1mSignature:\u001b[0m inject(*arg1)\n",
      "\u001b[1mNumber of lines:\u001b[0m 30\n",
      "\n",
      "Combines all elements of \u001b[1menum\u001b[0m by applying a binary\n",
      "operation, specified by a block or a symbol that names a\n",
      "method or operator.\n",
      "\n",
      "If you specify a block, then for each element in \u001b[1menum\u001b[0m\n",
      "the block is passed an accumulator value (\u001b[1mmemo\u001b[0m) and the element.\n",
      "If you specify a symbol instead, then each element in the collection\n",
      "will be passed to the named method of \u001b[1mmemo\u001b[0m.\n",
      "In either case, the result becomes the new value for \u001b[1mmemo\u001b[0m.\n",
      "At the end of the iteration, the final value of \u001b[1mmemo\u001b[0m is the\n",
      "return value for the method.\n",
      "\n",
      "If you do not explicitly specify an \u001b[1minitial\u001b[0m value for \u001b[1mmemo\u001b[0m,\n",
      "then the first element of collection is used as the initial value\n",
      "of \u001b[1mmemo\u001b[0m.\n",
      "\n",
      "\n",
      "   \u001b[1;34m# Sum some numbers\u001b[0m\n",
      "   (\u001b[1;34m5\u001b[0m..\u001b[1;34m10\u001b[0m).reduce(\u001b[33m:+\u001b[0m)                             \u001b[1;34m#=> 45\u001b[0m\n",
      "   \u001b[1;34m# Same using a block and inject\u001b[0m\n",
      "   (\u001b[1;34m5\u001b[0m..\u001b[1;34m10\u001b[0m).inject { |sum, n| sum + n }            \u001b[1;34m#=> 45\u001b[0m\n",
      "   \u001b[1;34m# Multiply some numbers\u001b[0m\n",
      "   (\u001b[1;34m5\u001b[0m..\u001b[1;34m10\u001b[0m).reduce(\u001b[1;34m1\u001b[0m, \u001b[33m:*\u001b[0m)                          \u001b[1;34m#=> 151200\u001b[0m\n",
      "   \u001b[1;34m# Same using a block\u001b[0m\n",
      "   (\u001b[1;34m5\u001b[0m..\u001b[1;34m10\u001b[0m).inject(\u001b[1;34m1\u001b[0m) { |product, n| product * n } \u001b[1;34m#=> 151200\u001b[0m\n",
      "   \u001b[1;34m# find the longest word\u001b[0m\n",
      "   longest = \u001b[31m\u001b[1;31m%w{\u001b[0m\u001b[31m cat sheep bear \u001b[1;31m}\u001b[0m\u001b[31m\u001b[0m.inject \u001b[32mdo\u001b[0m |memo, word|\n",
      "      memo.length > word.length ? memo : word\n",
      "   \u001b[32mend\u001b[0m\n",
      "   longest                                        \u001b[1;34m#=> \"sheep\"\u001b[0m\n"
     ]
    }
   ],
   "source": [
    "show-doc Array#inject"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題43. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#p String.instance_methods.grep(/strip/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題44. Hashの生成でエラーが発生するものを1つ選択してください。 \n",
    "\n",
    "1. capital ={:Sweden =>\"Stockholm\", :Norway=>\"Oslo\", :Finland=>\"Helsinki\"}\n",
    "1. capital = {Sweden: \"Stockholm\", Norway: \"Oslo\", Finland: \"Helsinki\"}\n",
    "1. capital = {\"Sweden\" = \"Stockholm\", \"Norway\" = \"Oslo\", \"Finland\" = \"Helsinki\"}\n",
    "1. capital = Hash[:Sweden, \"Stockholm\", :Norway ,\"Oslo\", :Finland, \"Helsinki\"]\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# capital ={:Sweden =>\"Stockholm\", :Norway=>\"Oslo\", :Finland=>\"Helsinki\"}\n",
    "# capital = {Sweden: \"Stockholm\", Norway: \"Oslo\", Finland: \"Helsinki\"}\n",
    "# capital = {\"Sweden\" = \"Stockholm\", \"Norway\" = \"Oslo\", \"Finland\" = \"Helsinki\"}\n",
    "# capital = Hash[:Sweden, \"Stockholm\", :Norway ,\"Oslo\", :Finland, \"Helsinki\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題45. 以下の出力になる時の`___(1)___`に入るものとして適切なものを1つ選択してください。\n",
    "\n",
    "```\n",
    "___(1)___ 'uri'\n",
    "uri = URI::HTTP.build({host:'www.ruby.or.jp', path:'/ja/certification/\n",
    "examination/'})\n",
    "puts uri\n",
    "[出力]\n",
    "http://www.ruby.or.jp/ja/certification/examination/\n",
    "```\n",
    "\n",
    "1. require\n",
    "1. include\n",
    "1. extend\n",
    "1. import\n",
    "\n",
    "〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "http://www.ruby.or.jp/ja/certification/examination/\n"
     ]
    }
   ],
   "source": [
    "# require 'uri'\n",
    "# include 'url'\n",
    "# extend 'url'\n",
    "# import 'url'\n",
    "\n",
    "# uri = URI::HTTP.build({host:'www.ruby.or.jp', path:'/ja/certification/examination/'})\n",
    "# puts uri"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題46. 以下の出力になる時の`___(1)___`に入るものとして適切なものを1つ選択してください。\n",
    "\n",
    "```\n",
    "class Foo\n",
    " ___(1)___= 0\n",
    " def self.count\n",
    " ___(1)___ += 1\n",
    " end\n",
    "end\n",
    "\n",
    "class Bar < Foo\n",
    "end\n",
    "\n",
    "puts Foo.count\n",
    "puts Bar.count\n",
    "[出力]\n",
    "1\n",
    "2\n",
    "```\n",
    "\n",
    "1. num\n",
    "1. @num\n",
    "1. @@num\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "class Foo\n",
    "#   num = 0\n",
    "#   @num = 0\n",
    "#  @@num = 0\n",
    "  def self.count\n",
    "#     num += 1\n",
    "#     @num += 1\n",
    "#    @@num += 1\n",
    "  end\n",
    "end\n",
    "\n",
    "class Bar < Foo\n",
    "end\n",
    "\n",
    "puts Foo.count\n",
    "puts Bar.count"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題47. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# unless false || nil\n",
    "#   print \"Hello\\n\"\n",
    "# end\n",
    "\n",
    "# unless false || true\n",
    "#   print \"Hello\\n\"\n",
    "# end\n",
    "\n",
    "# unless false && true\n",
    "#   print \"Hello\\n\"\n",
    "# end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題48. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× × 〇 〇\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# numbers = (1..20).to_a\n",
    "# p numbers.detect{|x| x % 5 == 0}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題49. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "```\n",
    "class NonasciiError < StandardError\n",
    "end\n",
    "File.open(\"sample.txt\") do |io|\n",
    "  io.each_line do |str|\n",
    "    begin\n",
    "      raise(NonasciiError, \"non ascii character detected\") unless str.ascii_only?\n",
    "    rescue => ex\n",
    "      puts \"#{ex.message} : #{str}\"\n",
    "    end\n",
    "  end\n",
    "end\n",
    "\n",
    "[sample.txtの内容]\n",
    "Ruby Association\n",
    "ルビーアソシエーション\n",
    "るびー\n",
    "Ruby on Rails\n",
    "```\n",
    "\n",
    "× 〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "### エンコーディングをUTF-8に設定\n",
    "# Encoding.default_external = \"UTF-8\"\n",
    "\n",
    "# Dir.mkdir(\"/tmp/tmp2\", 0755) unless Dir.exist?(\"/tmp/tmp2\")\n",
    "# Dir.chdir(\"/tmp/tmp2\")\n",
    "# File.open(\"sample.txt\",\"w\") do |io|\n",
    "#   io.write(\"Ruby Association\\n\")\n",
    "#   io.write(\"ルビーアソシエーション\\n\")\n",
    "#   io.write(\"るびー\\n\")\n",
    "#   io.write(\"Ruby on Rails\")\n",
    "# end\n",
    "\n",
    "# class NonasciiError < StandardError\n",
    "# end\n",
    "# File.open(\"/tmp/tmp2/sample.txt\") do |io|\n",
    "#  io.each_line do |str|\n",
    "#    begin\n",
    "#      raise(NonasciiError, \"non ascii character detected\") unless str.ascii_only?\n",
    "#    rescue => ex\n",
    "#      puts \"#{ex.message} : #{str}\"\n",
    "#    end\n",
    "#  end\n",
    "# end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題50. 10進数で10を表すものを2つ選択してください。\n",
    "\n",
    "1. 0xA\n",
    "1. 0xFF\n",
    "1. 012\n",
    "1. 077\n",
    "1. 0x10\n",
    "\n",
    "× 〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# p 0xA\n",
    "# p 0xFF\n",
    "# p 012\n",
    "# p 077\n",
    "# p 0x10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# Ruby技術者認定資格合格教本"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題1. マジックコメントの書き方で正しいものをすべて選んでください\n",
    "\n",
    "1. `# coding: utf-8`\n",
    "1. `# encoding: utf-8`\n",
    "1. `# -*- charset: utf-8 -*-`\n",
    "1. `# CODING: UTF-8`\n",
    "\n",
    "〇 〇 〇\n",
    "\n",
    "<!-- 1,2,4 -->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題2. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× × 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# x = 0\n",
    "# def hoge\n",
    "#   (1...5).each do |i|\n",
    "#     x += 1\n",
    "#   end\n",
    "#   puts x\n",
    "# end\n",
    "# hoge"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題3. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# begin\n",
    "#   puts 1+\"2\"\n",
    "# rescue\n",
    "#   puts \"Error.\"\n",
    "# rescue TypeError\n",
    "#   puts \"TypeError.\"\n",
    "# ensure\n",
    "#   puts \"Ensure.\"\n",
    "# end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題4. エラーになるのはどれですか\n",
    "\n",
    "1. puts \"90\"\n",
    "1. puts '90'\n",
    "1. puts 0x90\n",
    "1. puts 090\n",
    "\n",
    "進数に注意\n",
    "\n",
    "〇 × × ○"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 117,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# puts \"90\"\n",
    "# puts '90'\n",
    "# puts 0x90\n",
    "# puts 090\n",
    "\n",
    "\n",
    "\n",
    "# puts 0o70\n",
    "# puts 0d10\n",
    "# puts 0b10"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題5. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 151,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# x = 10\n",
    "# y = x < 10 ? \"A\" : \"B\"\n",
    "# puts y"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題6. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 153,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# MAX=10\n",
    "# print MAX\n",
    "# MAX=100\n",
    "# print MAX"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題7. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "☓ 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# def foo(*a)\n",
    "#   p a\n",
    "# end\n",
    "\n",
    "# foo(1,2,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題8. 以下の変数の說明のうち間違っているものを全て選んでください\n",
    "\n",
    "1.  ローカル変数はアルファベット1文字で始まり、2文字以上である\n",
    "1. グローバル変数は `$` で始まる\n",
    "1. インスタンス変数は `@` で始まる\n",
    "1. クラス変数は `*` で始まる\n",
    "1. 定数はアルファベット大文字で始まる\n",
    "\n",
    "○ 〇 〇\n",
    "\n",
    "<!-- 1,4 -->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題9. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "○ 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 121,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# class Hoge\n",
    "#   attr_reader :message\n",
    "#   def initialize\n",
    "#     @message = \"Hello\"\n",
    "#   end\n",
    "# end\n",
    "\n",
    "# class Piyo < Hoge\n",
    "#   def initialize\n",
    "#     @message = \"Hi\"\n",
    "#     super\n",
    "#   end\n",
    "# end\n",
    "\n",
    "# puts Piyo.new.message"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題10. 円の面積を求めるコードを動かすために必要な修正をしてください(2通り)\n",
    "\n",
    "```\n",
    "def area r\n",
    "  return r * r * PI\n",
    "end\n",
    "p area(3)\n",
    "```\n",
    "\n",
    "× 〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28.274333882308138\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "28.274333882308138"
      ]
     },
     "execution_count": 122,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def area r\n",
    "  #return r * r * (hoge)PI\n",
    "end\n",
    "p area(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "28.274333882308138\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "28.274333882308138"
      ]
     },
     "execution_count": 123,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# hoge Math\n",
    "# def area r\n",
    "#  return r * r * PI\n",
    "# end\n",
    "# p area(3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題11. オーバーライド不可の演算子を2つ選んでください\n",
    "\n",
    "1. +\n",
    "2. ..\n",
    "3. %\n",
    "4. &\n",
    "5. &&\n",
    "6. ==\n",
    "\n",
    "× × × 〇\n",
    "\n",
    "<!--\n",
    "=\n",
    "?\n",
    ":\n",
    "..\n",
    "...\n",
    "&&\n",
    "||\n",
    "and\n",
    "or\n",
    "not\n",
    "::\n",
    "-->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題12. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "○ × 〇\n",
    "\n",
    "<!-- クラス拡張よりも特異メソッドが優先される -->"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# s = \"Hello\"\n",
    "\n",
    "# def s.greet\n",
    "#   puts \"Hi!\"\n",
    "# end\n",
    "\n",
    "# class String\n",
    "#   def greet\n",
    "#     puts \"Hello!\"\n",
    "#   end\n",
    "# end\n",
    "\n",
    "# s.greet"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# ?問題13. 実行結果になるように `___X___` に入るコードを入れてください\n",
    "\n",
    "```\n",
    "1.\n",
    "================================================\n",
    "def <=> other\n",
    "  return self.id <=> other.id\n",
    "end\n",
    "================================================\n",
    "\n",
    "2.\n",
    "================================================\n",
    "def compareTo(other)\n",
    "  return self.compareTo(other)\n",
    "================================================\n",
    "\n",
    "3.\n",
    "================================================\n",
    "include Sortable\n",
    "================================================\n",
    "\n",
    "4.\n",
    "================================================\n",
    "def sort(self, other)\n",
    "  self.id <=> other.id\n",
    "end\n",
    "================================================\n",
    "```\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 127,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1:Suzuki\n",
      "2:Sato\n",
      "3:Tanaka\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[#<Employee:0x007f4b694cd0b8 @id=\"1\", @name=\"Suzuki\">, #<Employee:0x007f4b694cd040 @id=\"2\", @name=\"Sato\">, #<Employee:0x007f4b694cd180 @id=\"3\", @name=\"Tanaka\">]"
      ]
     },
     "execution_count": 127,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Employee\n",
    "  attr_reader :id\n",
    "  attr_reader :name\n",
    "  #attr_accessor :name\n",
    "  def initialize id, name\n",
    "    @id = id\n",
    "    @name = name\n",
    "  end\n",
    "  \n",
    "  def to_s\n",
    "    return \"#{@id}:#{@name}\"\n",
    "  end  \n",
    "  # ___X___\n",
    "#   def <=> other\n",
    "#    return self.id <=> other.id\n",
    "#   end\n",
    "end\n",
    "\n",
    "employees = []\n",
    "employees << Employee.new(\"3\",\"Tanaka\")\n",
    "employees << Employee.new(\"1\",\"Suzuki\")\n",
    "employees << Employee.new(\"2\",\"Sato\")\n",
    "employees.sort!\n",
    "\n",
    "employees.each do |employees|\n",
    "  puts employees\n",
    "end\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題14. 実行結果になるように `___X___` に入るコードを入れてください\n",
    "\n",
    "```\n",
    "a = [1,2,3,4]\n",
    "b = [1,3,5,7]\n",
    "\n",
    "c = ___X___\n",
    "c.each {|i| print i, \" \"}\n",
    "\n",
    "### 実行結果\n",
    "1 3\n",
    "```\n",
    "\n",
    "〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 128,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1 3 "
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 3]"
      ]
     },
     "execution_count": 128,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# a = [1,2,3,4]\n",
    "# b = [1,3,5,7]\n",
    "\n",
    "# c = hoge\n",
    "# c.each {|i| print i, \" \"}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題15. 実行結果になるように `___X___` に入るコードを入れてください\n",
    "\n",
    "```\n",
    "a = [1,2,3,4]\n",
    "\n",
    "___X___.each do |i| print i, \" \" end\n",
    "\n",
    "### 実行結果\n",
    "1 2 3\n",
    "```\n",
    "\n",
    "× 〇 × 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 4]"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,2,3,4]\n",
    "\n",
    "#a[0..-1].each do |i| print i, \" \" end\n",
    "#a[0...-1].each do |i| print i, \" \" end\n",
    "# a[0,1].each do |i| print i, \" \" end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題16. 配列のメソッドについて適切な記述を2つ選んでください\n",
    "\n",
    "1. find_allとselectは同じ動作をする。\n",
    "1. mapとcollectは同じ動作をする。\n",
    "1. findとselectは同じ動作をする\n",
    "1. firstは先頭の要素を削除し、その要素を返す。\n",
    "1. concatは非破壊的メソッドである\n",
    "1. concat!は破壊的メソッドである\n",
    "\n",
    "× 〇 〇\n",
    "\n",
    "<!-- 1,2 -->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題17. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× 〇 × 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 157,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# p a1=%w(a b)\n",
    "# p a2=%w(x y)\n",
    "# p a3=a1.zip(a2)\n",
    "# p a3.first"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題18. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# a = [1,2,3,4,5]\n",
    "# p a.slice(1,3)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題19. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 × 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# a = \"abc\"\n",
    "# b = \"abc\"\n",
    "\n",
    "# puts a.eql? b\n",
    "# puts a.equal? b\n",
    "# puts a == b\n",
    "# puts a === b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題20. 以下のコードでエラーにならないものはどれですか。\n",
    "\n",
    "× 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# puts 5 + \"Hello\"\n",
    "# puts \"Hello\" + 5\n",
    "# puts \"Hello\" * 5\n",
    "# puts 5 * \"Hello\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題21. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× × × 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 148,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# s = <<\"EOB\"\n",
    "# Hello,\n",
    "# Ruby\n",
    "# World.\n",
    "# EOB\n",
    "# \"EOB\"\n",
    "# puts s\n",
    "# #p s"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題22. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× × 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 156,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# s1=\"Hoge\"\n",
    "# s2=\"Fuga\"\n",
    "# p s1.concat(s2)\n",
    "# p s1.chop\n",
    "# p s1.chomp\n",
    "# p s1+s2\n",
    "# puts s1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題23. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇  〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 155,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# s = \"123456789\"\n",
    "# p s[1,4]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題24. 実行結果になるように `___X___` に入るコードを入れてください\n",
    "\n",
    "```\n",
    "member = [10, \"Tanaka\"]\n",
    "print \"ID:%2d Name:%s\"___X___member\n",
    "\n",
    "===\n",
    "ID:10 Name:Tanaka\n",
    "```\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 233,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# member = [10, \"Tanaka\"]\n",
    "# print \"ID:%2d Name:%s\" hoge member"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題25. Hashクラスについて適切な記述を2つ選んでください\n",
    "\n",
    "1. {}で空のHashオブジェクトを生成できる\n",
    "1. []で空のHashオブジェクトを生成できる\n",
    "1. invertでキーと値を入れ替える事ができる\n",
    "1. keyに対応する値を取り出すメソッドとしてhas_key?(key)、key?(key)、get_key(key)などが用意されている\n",
    "1. 全ての要素を削除するには、delete_allを使用する\n",
    "\n",
    "〇 〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# a = {a: \"hoge\", b: \"fuga\"}\n",
    "# a.invert\n",
    "# puts a.has_key?(:a)\n",
    "# puts a.key?(:a)\n",
    "# puts a.include?(:a)\n",
    "# puts a.member?(:a)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題26. Hashクラスの説明として適切ではない記述はどれですか\n",
    "\n",
    "1. deleteはキーを指定してキーと値の組を削除する。\n",
    "1. removeはキーと値の組を削除する。\n",
    "1. fetchは与えられたキーに関連付けられた値を返す。\n",
    "1. clearはハッシュの要素をすべて削除する。\n",
    "\n",
    "× × × 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "ename": "KeyError",
     "evalue": "key not found: :c",
     "output_type": "error",
     "traceback": [
      "\u001b[31mKeyError\u001b[0m: key not found: :c",
      "\u001b[37m(pry):307:in `fetch'\u001b[0m",
      "\u001b[37m(pry):307:in `<main>'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/backend.rb:66:in `eval'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/backend.rb:12:in `eval'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:87:in `execute_request'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:47:in `dispatch'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:37:in `run'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/command.rb:70:in `run_kernel'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/command.rb:34:in `run'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/bin/iruby:5:in `<top (required)>'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/bin/iruby:23:in `load'\u001b[0m",
      "\u001b[37m/Users/ftakao2007/work/jupyter/vendor/bundle/ruby/2.3.0/bin/iruby:23:in `<main>'\u001b[0m"
     ]
    }
   ],
   "source": [
    "h = {a: \"hoge\", b: \"fuga\"}\n",
    "# h.delete(:a)\n",
    "# h.remove(:a)\n",
    "# puts h.fetch(:a)\n",
    "# puts h.fetch(:c)\n",
    "# h.clear\n",
    "# puts h"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題27. 実行結果になるように `___X___` に入るコードを入れてください\n",
    "\n",
    "```\n",
    "h = ___X___\n",
    "p h\n",
    "\n",
    "===\n",
    "{ :a => 1, :b => 2, :c => 3 }\n",
    "```\n",
    "\n",
    "〇 〇\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 176,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# a = {a: 1, b: 2, c: 3}\n",
    "# puts a\n",
    "# p Hash[:a, 1, :b, 2, :c, 3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題28. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "〇 ×"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# ## 危険\n",
    "# File.open(\"data\") do |io|\n",
    "#   while not io.eof?\n",
    "#     print io.read(1)\n",
    "#     io.seek(0,IO::SEEK_SET)\n",
    "#     sleep 1\n",
    "#   end\n",
    "# end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題29. 実行結果になるように `___X___` に入るコードを入れてください\n",
    "\n",
    "```\n",
    "t = Time.gm(1970,1,1)\n",
    "puts t.strftime(\"___X___\")\n",
    "\n",
    "===\n",
    "1970/01/01\n",
    "```\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# t = Time.gm(1970,1,1)\n",
    "# puts t.strftime(\"%Y/%m/%d\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題30. Newまたはnewのみで構成される行にマッチする正規表現を2つ選んでください\n",
    "\n",
    "1. `^[Nn]ew$`\n",
    "1. `^N|new$`\n",
    "1. `^[Nn][e][w]$`\n",
    "1. `^[New][new]$`\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 模擬試験"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題1. Rubyの予約語として適切なものはどれですか(2つ)\n",
    "\n",
    "1. while\n",
    "1. rand\n",
    "1. goto\n",
    "1. class\n",
    "1. const\n",
    "\n",
    "〇 〇 〇\n",
    "\n",
    "<!-- 1,4 -->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題2. 以下のコードを実行した出力はどうなりますか。\n",
    "\n",
    "× 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# x = 0\n",
    "# [1,2,3].each do |x|\n",
    "#   #print x + \" \"\n",
    "#   print x.to_s + \" \"\n",
    "# end\n",
    "# puts x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題3. Rubyにおける偽の値として正しいものはどれですか。(2つ)\n",
    "\n",
    "1. nil\n",
    "1. 0\n",
    "1. \"\"\n",
    "1. false\n",
    "1. 0.0\n",
    "1. NULL\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題4. 実行結果になるように `___X___` に入るコードを入れてください\n",
    "\n",
    "× 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "succeeded!\n"
     ]
    }
   ],
   "source": [
    "y = false\n",
    "# y | raise\n",
    "# y || raise \n",
    "# y & raise \n",
    "# y && raise \n",
    "puts (\"succeeded!\")\n",
    "\n",
    "\n",
    "# y | raise \"failed\"\n",
    "# y || raise \"failed\"\n",
    "# y & raise \"failed\"\n",
    "# y && raise \"failed\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題5. 1から10まで表示するコードで正しいものはどれですか\n",
    "\n",
    "1. (1.10).each do |i| puts i end\n",
    "1. (1..10).each do |i| puts i end\n",
    "1. (1...10).each do |i| puts i end\n",
    "1. (1....10).each do |i| puts i end\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題6. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "× 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 195,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# HOGE = \"hoge\"\n",
    "# HOGE.gsub!(\"hoge\",\"piyo\")\n",
    "# print HOGE\n",
    "\n",
    "# FUGA = \"hoge\"\n",
    "# FUGA << \"piyo\"\n",
    "# print FUGA"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題7. 例外について正しいものはどれですか\n",
    "\n",
    "1. beginによって例外を補足する場合は、例外の可能性がある処理をbeginからrescueの間に記述する\n",
    "1. ひとつの処理に対して複数のrescureは指定できない\n",
    "1. 例外オブジェクトのメソッドstacktraceにより例外スタックを取得できる\n",
    "1. ensureは必ず記述する必要がある\n",
    "\n",
    "〇　〇"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題8. ZeroDivisionErrorが発生したときの処理で正しい書き方はどれですか\n",
    "\n",
    "1. rescure ZeroDivisionError = ex\n",
    "1. rescure ZeroDivisionError ex\n",
    "1. rescure ZeroDivisionError -> ex\n",
    "1. rescure ZeroDivisionError => ex\n",
    "\n",
    "〇　〇"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題9. 以下のコードは何が出力されますか\n",
    "\n",
    "```\n",
    "p ?A\n",
    "```\n",
    "\n",
    "〇　〇"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題10. 以下のうちエラーにならないものはどれですか\n",
    "\n",
    "1. class Hoge < Object ; end\n",
    "1. class Hoge << Object ; end\n",
    "1. class Hoge < Kernel ; end\n",
    "1. class Hoge << Kernel ; end\n",
    "\n",
    "× 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 199,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "ename": "TypeError",
     "evalue": "superclass must be a Class (Module given)",
     "output_type": "error",
     "traceback": [
      "\u001b[31mTypeError\u001b[0m: superclass must be a Class (Module given)",
      "\u001b[37m(pry):1251:in `<main>'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:355:in `evaluate_ruby'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:323:in `handle_line'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:243:in `block (2 levels) in eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `catch'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:242:in `block in eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `catch'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/pry-0.10.4/lib/pry/pry_instance.rb:241:in `eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/backend.rb:66:in `eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/backend.rb:12:in `eval'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:87:in `execute_request'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:47:in `dispatch'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/kernel.rb:37:in `run'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/command.rb:70:in `run_kernel'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/lib/iruby/command.rb:34:in `run'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/gems/iruby-0.3/bin/iruby:5:in `<top (required)>'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/bin/iruby:22:in `load'\u001b[0m",
      "\u001b[37m/root/jupyter/vendor/bundle/ruby/2.3.0/bin/iruby:22:in `<main>'\u001b[0m"
     ]
    }
   ],
   "source": [
    "# class Hoge < Object  ; end\n",
    "# class Hoge << Object  ; end\n",
    "# class Hoge < Kernel  ; end\n",
    "# class Hoge << Kernel  ; end"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題11. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 203,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# class Parent\n",
    "#   attr_reader :name\n",
    "#   def initialize name\n",
    "#     @name = name\n",
    "#   end\n",
    "# end\n",
    "\n",
    "# class Child < Parent\n",
    "#   def initialize name\n",
    "#     @name = \"Child\"+name\n",
    "#   end\n",
    "# end\n",
    "\n",
    "# puts Child.new(\"Hoge\").name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題12. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "〇　〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 204,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "16"
     ]
    }
   ],
   "source": [
    "class Integer\n",
    "  def to_square\n",
    "    return self * self\n",
    "  end\n",
    "end\n",
    "\n",
    "print 4.to_square"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 208,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class String\n",
    "  def hogesum\n",
    "    return self.to_i + 3\n",
    "  end\n",
    "end\n",
    "\n",
    "\"2\".hogesum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 244,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "fugahoge\n",
      "\n",
      "\n",
      "fugaself\n",
      "fugaself\n",
      "\n",
      "[:fugaself]\n",
      "[:fuga, :fugaself]\n"
     ]
    }
   ],
   "source": [
    "class Fuga\n",
    "  ### インスタンスメソッド\n",
    "  def fuga\n",
    "    puts \"fugahoge\"\n",
    "  end\n",
    "  \n",
    "  ### クラスメソッド\n",
    "  def self.fugaself\n",
    "    puts \"fugaself\"\n",
    "  end\n",
    "end\n",
    "\n",
    "### インスタンスメソッドはインスタンス化すれば使える\n",
    "b = Fuga.new\n",
    "puts b.fuga\n",
    "puts\n",
    "\n",
    "### クラスメソッドはクラスに直接.をつけてメソッドを呼び出せる\n",
    "Fuga.fugaself\n",
    "### ::も可\n",
    "Fuga::fugaself\n",
    "puts\n",
    "\n",
    "puts Fuga.methods.grep(/fuga/)\n",
    "puts b.methods.grep(/fuga/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題13. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 245,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "12\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "12"
      ]
     },
     "execution_count": 245,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# p \"12abc\".to_i"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題14. 以下の結果になるように__X__にコードを記述してください\n",
    "\n",
    "〇 〇\n",
    "\n",
    "```\n",
    "a=[1,2,3,4]\n",
    "p __X__\n",
    "\n",
    "[2,3]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 251,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# a=[1,2,3,4]\n",
    "# a[1,3]\n",
    "# a[1..3]\n",
    "# a[1...3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題15. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 253,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# a=[1,2,3,4,5]\n",
    "# p a[2..-1]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題16. 以下の結果になるように__X__にコードを記述してください\n",
    "\n",
    "〇 〇\n",
    "\n",
    "```\n",
    "a=1,2,3\n",
    "p __X__\n",
    "\n",
    "\"1,2,3\"\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 258,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# a=1,2,3\n",
    "# p a.to_s(\",\")\n",
    "# p a.join(\",\")\n",
    "# p a.concat(\",\")\n",
    "# p a.to_a(\",\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題17. 以下の結果になるように__X__にコードを記述してください\n",
    "\n",
    "〇 × ○\n",
    "\n",
    "```\n",
    "s=\"a;b;c;d;e;f\"\n",
    "p s.split(__X__)\n",
    "\n",
    "[\"a\",\"b\",\"c\",\"d\",\"e\",\"f\"]\n",
    "```\n",
    "\n",
    "1. /:|;/\n",
    "1. \":|;\"\n",
    "1. \"/:|;/\"\n",
    "1. :|;\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# s=\"a;b;c;d;e;f\"\n",
    "# p s.split()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題18. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "× × ○"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "[1, 3]\n",
      "[1, 3, 1, 3, 5, 1, 2, 3]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[1, 3, 1, 3, 5, 1, 2, 3]"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# a = [1,2,3]\n",
    "# b = [1,3,5]\n",
    "# puts c = a\n",
    "# puts a = b & c\n",
    "# p a + b + c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 278,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "69980927737220\n",
      "69980927737220\n",
      "\n",
      "69980927671280\n",
      "69980927737200\n",
      "69980927737220\n",
      "[1, 3]\n",
      "[1, 3, 5]\n",
      "[1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3]\n",
    "b = [1,3,5]\n",
    "c = a\n",
    "puts a.object_id\n",
    "puts c.object_id\n",
    "puts\n",
    "\n",
    "a = b & c\n",
    "puts a.object_id\n",
    "puts b.object_id\n",
    "puts c.object_id\n",
    "\n",
    "puts a\n",
    "puts b\n",
    "puts c"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3]\n",
      "[1, 2, 3]\n",
      "\n",
      "[5, 2, 3]\n",
      "[5, 2, 3]\n",
      "\n",
      "[5]\n",
      "[5, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "a = [1,2,3]\n",
    "b = a\n",
    "puts a\n",
    "puts b\n",
    "puts\n",
    "\n",
    "a[0] = 5\n",
    "puts a\n",
    "puts b\n",
    "puts \n",
    "\n",
    "a = [5]\n",
    "puts a\n",
    "puts b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題19. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "×　× ○"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# a = [1,2,3,4]\n",
    "# b = [1,3,5,7]\n",
    "\n",
    "# p a || b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3, 5, 8]"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [1,3,4,8]\n",
    "b = [1,2,3,5,8]\n",
    "\n",
    "# p a | b\n",
    "# p a & b\n",
    "# p a || b\n",
    "# p a && b"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題20. 以下の結果になるように__X__にコードを記述してください\n",
    "\n",
    "〇 〇\n",
    "\n",
    "```\n",
    "def sum (__X__)\n",
    "  total = 0\n",
    "  a.each {|i| total +=i }\n",
    "  return total\n",
    "end\n",
    "puts sum(1,2,3,4,5)\n",
    "\n",
    "15\n",
    "```\n",
    "\n",
    "1. a\n",
    "1. *a\n",
    "1. a[]\n",
    "1. &a\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 288,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# def sum\n",
    "#   total = 0\n",
    "#   a.each {|i| total +=i }\n",
    "#   return total\n",
    "# end\n",
    "# puts sum(1,2,3,4,5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題21. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "〇 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 291,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# a=[1,2,3]\n",
    "# b=[1,3,5]\n",
    "# c=[2,3,4]\n",
    "# p a+b-c"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題22. Arrayクラスでdelete_ifと同じ動作のメソッドは何でしょうか\n",
    "\n",
    "(答えはここにコメントアウトしてある)\n",
    "\n",
    "× 〇\n",
    "\n",
    "<!--\n",
    "a = [10,20,30,40,50]\n",
    "#a.delete_if{|x| x < 25}\n",
    "puts a.reject{|x| x < 25}\n",
    "#puts a.reject!{|x| x < 25}\n",
    "puts a\n",
    "-->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題23. 以下の結果になるように__X__にコードを記述してください\n",
    "\n",
    "〇 〇\n",
    "\n",
    "```\n",
    "a=[\"apple\",\"orange\",\"grape\",\"pine\"]\n",
    "a.__X__ {|item,i| print i,\":\",item,\"\\n\"}\n",
    "\n",
    "0:apple\n",
    "1:orange\n",
    "2:grape\n",
    "3:pine\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題24. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "× ☓ 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# s = [\"one\",\"two\",\"three\"]\n",
    "# s.shift\n",
    "# s.shift\n",
    "# s.unshift\n",
    "# s.push \"four\"\n",
    "# p s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "[2, 3, 4, 5, 6, 7, 8, 9]\n",
      "\n",
      "[2, 3, 4]\n",
      "[5, 6, 7, 8, 9]\n",
      "\n",
      "[5, 6, 7, 8, 9]\n",
      "[5, 6, 7, 8, 9]\n",
      "\n",
      "[\"a\", \"b\", 5, 6, 7, 8, 9]\n",
      "[\"a\", \"b\", 5, 6, 7, 8, 9]\n",
      "\n",
      "[\"a\", \"b\", 5, 6, 7, 8, 9, 10]\n"
     ]
    }
   ],
   "source": [
    "s = [1,2,3,4,5,6,7,8,9]\n",
    "puts s.shift\n",
    "puts s\n",
    "puts\n",
    "\n",
    "puts s.shift(3)\n",
    "puts s\n",
    "puts\n",
    "\n",
    "\n",
    "puts s.unshift\n",
    "puts s\n",
    "puts\n",
    "\n",
    "puts s.unshift(\"a\",\"b\")\n",
    "puts s\n",
    "puts\n",
    "\n",
    "puts s.push(10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題25. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "× ☓ 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# a = [:a,:a,:b,:c]\n",
    "# a[5]=:e\n",
    "# a.concat([:a,:b,:c])\n",
    "# a.compact\n",
    "# a.uniq\n",
    "# puts a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 284,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\"Cat\", \"dog\", \"mouse\"]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[\"Cat\", \"dog\", \"mouse\"]"
      ]
     },
     "execution_count": 284,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "animals = [\"Cat\", \"dog\", \"cat\", \"mouse\", \"Dog\"]\n",
    "animals.uniq! {|name| name.downcase }\n",
    "p animals"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題26. Arrayクラスでdetectと同じ動作をするメソッドは何ですか\n",
    "\n",
    "〇 ○ 〇\n",
    "\n",
    "<!--\n",
    "  find\n",
    "-->"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題27. 以下の結果になるように__X__にコードを記述してください\n",
    "\n",
    "〇 ○ 〇\n",
    "\n",
    "```\n",
    "a=[1,2,3,4,5,6]\n",
    "p __X__\n",
    "\n",
    "[2,3,4,5,10,12]\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 301,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# a=[1,2,3,4,5,6]\n",
    "# p a.hoge{|v| v*2}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題28. 以下の結果になるように__X__にコードを記述してください\n",
    "\n",
    "× ○ 〇\n",
    "\n",
    "```\n",
    "sarray = __X__\n",
    "sarray.each {|v| print v, \" \"}\n",
    "\n",
    "Apple Orange Grape\n",
    "```\n",
    "\n",
    "1. %w(Apple Orange Grape)\n",
    "1. %w(Apple, Orange, Grape)\n",
    "1. %w(\"Apple\" \"Orange\" \"Grape\")\n",
    "1. %w(\"Apple\",\"Orange\",\"Grape\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 304,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# sarray = %w()\n",
    "# sarray.each {|v| print v, \" \"}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題29. 以下の結果になるように__X__にコードを記述してください\n",
    "\n",
    "× ☓ 〇\n",
    "\n",
    "```\n",
    "a = [\"a\", \"b\", \"c\"]\n",
    "b = [1,2,3]\n",
    "__X__\n",
    "\n",
    "[\"a\", 1]\n",
    "[\"b\", 2]\n",
    "[\"c\", 3]\n",
    "```\n",
    "\n",
    "1. a.zip(b).each{|x| p x}\n",
    "1. a.zip(b){|x| p x}\n",
    "1. [a,b].zip{|x,y| p [x,y]}\n",
    "1. [a,b].transpose.each{|x,y| p [x,y] }\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 310,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[1, 2, 3]"
      ]
     },
     "execution_count": 310,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = [\"a\", \"b\", \"c\"]\n",
    "b = [1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 268,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1, 4, 6], [2, 5, 7], [3, nil, 8]]\n",
      "\n",
      "[1, 4, 6]\n",
      "[2, 5, 7]\n",
      "[3, nil, 8]\n",
      "\n",
      "[1, 4, 6]\n",
      "[2, 5, 7]\n",
      "[3, nil, 8]\n",
      "\n",
      "element size differs (2 should be 3)\n",
      "\n",
      "[1, 4, 7]\n",
      "[2, 5, 8]\n",
      "[3, 6, 9]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "arr1 = [1, 2, 3]\n",
    "arr2 = [4, 5]\n",
    "arr3 = [6, 7, 8, 9]\n",
    "\n",
    "p arr1.zip(arr2, arr3)\n",
    "puts\n",
    "\n",
    "arr1.zip(arr2, arr3).each{|x| puts x}\n",
    "puts \n",
    "\n",
    "arr1.zip(arr2, arr3){|x| puts x}\n",
    "puts\n",
    "\n",
    "begin\n",
    "  [arr1,arr2, arr3].transpose.each{|x,y,z| p [x,y,z]}\n",
    "rescue IndexError\n",
    "  puts $!\n",
    "end\n",
    "puts\n",
    "\n",
    "arr1 = [1, 2, 3]\n",
    "arr2 = [4, 5, 6]\n",
    "arr3 = [7, 8, 9]\n",
    "[arr1,arr2, arr3].transpose.each{|x,y,z| p [x,y,z]}\n",
    "puts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 267,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[[1, 2], [3, 4]]"
      ]
     },
     "execution_count": 267,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "[[1, 2], [3, 4]].each{|x| x.each{|y| puts y}}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題30. 以下のコードは正しく動作しません。正しく動作するように修正してください\n",
    "\n",
    "```\n",
    "s = << EOB\n",
    "Hello, Ruby World.\n",
    "Hi, Ruby World.\n",
    "Goodbye, Ruby World.\n",
    "  EOB\n",
    "```\n",
    "〇 ○"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 238,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# s = <<EOB\n",
    "# Hello, Ruby World.\n",
    "# Hi, Ruby World.\n",
    "# Goodbye, Ruby World.\n",
    "#   EOB"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題31. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "〇 ○"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# puts \"hello\".encoding.name"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題32. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "〇 ☓ 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 314,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# a = \"Ruby\"\n",
    "# b = \"Rails\"\n",
    "# b = a\n",
    "# a.upcase\n",
    "# print b"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 233,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "代入は別オブジェクトになる\n",
      "70354819788800\n",
      "70354819788800\n",
      "70354819756900\n",
      "70354819788800\n",
      "\n",
      "破壊的メソッドは同じオブジェクトのまま\n",
      "70354812323220\n",
      "70354812323220\n",
      "70354812323220\n",
      "70354812323220\n"
     ]
    }
   ],
   "source": [
    "puts \"代入は別オブジェクトになる\"\n",
    "a = \"hoge\"\n",
    "b = a\n",
    "puts a.object_id\n",
    "puts b.object_id\n",
    "\n",
    "a = \"fuga\"\n",
    "puts a.object_id\n",
    "puts b.object_id\n",
    "puts\n",
    "\n",
    "puts \"破壊的メソッドは同じオブジェクトのまま\"\n",
    "c = \"fuga\"\n",
    "d = c\n",
    "puts c.object_id\n",
    "puts d.object_id\n",
    "\n",
    "c.upcase!\n",
    "puts c.object_id\n",
    "puts d.object_id\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題33. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "× ○ 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 315,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "9"
      ]
     },
     "execution_count": 315,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# p \"find!find!find!find!find!find\".index(\"!\",5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題34. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "× ○"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 317,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# x = \"Hello, World.\\n\"\n",
    "# p x.chop\n",
    "# p x.chop\n",
    "# p x.chomp\n",
    "# p x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題35. 【この行】を入れ替えても同じになるものはどれですか\n",
    "\n",
    "```\n",
    "a = \"abcdefghijk\"\n",
    "a[1,3] = \"x\"  # 【この行】\n",
    "print a,\"\\n\"\n",
    "\n",
    "a[1..3] = \"x\"\n",
    "a[1...3] = \"x\"\n",
    "a[123] = \"x\"\n",
    "a[1-3] = \"x\"\n",
    "```\n",
    "\n",
    "〇 ○ 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 208,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# a = \"abcdefghijk\"\n",
    "# a[1,3] = \"x\"  # 【この行】\n",
    "\n",
    "# print a,\"\\n\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題36. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "× ☓ 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# p \"hogepiyohogehoge\".slice(/o../)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 178,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#show-doc String#slice"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題37. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "× ○　× ☓"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#puts \"0123456789-\".delete(\"^13-56-\")\n",
    "#puts \"0123456789-\".delete(\"13-56-\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題38. 数字のみで構成される行にマッチする正規表現はどれか\n",
    "\n",
    "1. `/^[0-9].$/`\n",
    "1. `/^[0-9]*$/`\n",
    "1. `/^[0-9][0-9]*$/`\n",
    "1. `/^[0-9][0-9].*$/`\n",
    "\n",
    "×　☓ 〇"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題39. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "× ☓ 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#p \"abc def 123 ghi 456\".scan(/\\d+/).length\n",
    "#p \"abc def 123 ghi 456\".scan(/\\d/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 327,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[\"of\", \"the\", \"people\", \"123\", \"by\", \"the\", \"people\", \"for\", \"the\", \"people\"]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "[\"of\", \"the\", \"people\", \"123\", \"by\", \"the\", \"people\", \"for\", \"the\", \"people\"]"
      ]
     },
     "execution_count": 327,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = \"of the people, 123 by  あいう the people, for the people % &\"\n",
    "p s.scan(/\\w+/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題40. 以下の実行結果になるように__X__と__Y__入れてください\n",
    "\n",
    "```\n",
    "p 65.___X___\n",
    "p \"A\".___Y___\n",
    "\n",
    "\"A\"\n",
    "65\n",
    "```\n",
    "\n",
    "〇 ○ 〇"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題41. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "× ○ 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 333,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# p \"HogeHOGEhoge\"[/[A-Z][^A-Z]+/]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題42. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "〇 ○ 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 334,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# h = {\"a\": 1, \"b\": 2, \"c\": 3}\n",
    "# p h"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題43. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "〇 ○ 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 337,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1=>\"Hoge\", 2=>\"Piyo\", 3=>\"fuga\"}"
      ]
     },
     "execution_count": 337,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h = {1 => \"Hoge\", 2 => \"Piyo\", 3 => \"fuga\"}\n",
    "#p h.invert\n",
    "#p h.to_a"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題44. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "× ○ 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{1=>\"Hoge\", 2=>\"Piyo\", 3=>\"fuga\"}"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "h = {1 => \"Hoge\", 2 => \"Piyo\", 3 => \"fuga\"}\n",
    "# p h.reject{|x,y| x < 2}\n",
    "# p h\n",
    "\n",
    "# puts h.delete(2)\n",
    "# puts h"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題45. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "× ○ × ○"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[\"Baz\", \"fuga\"], [\"Foo\", \"hoge\"], [\"Bar\", \"piyo\"]]\n",
      "{\"Foo\"=>\"hoge\", \"Bar\"=>\"piyo\", \"Baz\"=>\"fuga\"}\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "{\"Foo\"=>\"hoge\", \"Bar\"=>\"piyo\", \"Baz\"=>\"fuga\"}"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = {\"Foo\" => \"Hoge\", \"Bar\" => \"Piyo\", \"Baz\" => \"Fuga\"}\n",
    "b = {\"Foo\" => \"hoge\", \"Bar\" => \"piyo\", \"Baz\" => \"fuga\"}\n",
    "\n",
    "# p a.update(b)\n",
    "# p b.update(a)\n",
    "# p a.update(b).sort{|a,b| a[1] <=> b[1]}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題46. Dirクラスのメソッドではないものを選んでください\n",
    "\n",
    "1. Dir.pwd\n",
    "1. Dir.extname\n",
    "1. Dir.open\n",
    "1. Dir.mkdir\n",
    "1. Dir.rmdir\n",
    "1. Dir.chdir\n",
    "\n",
    "× ○ 〇"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題47. 文字を逆順に書き込む処理を行うとき__X__に入るものは何になりますか\n",
    "\n",
    "\n",
    "```\n",
    "open(\"test.txt\",\"__X__\") do |f|\n",
    "  data = f.read.chomp\n",
    "  data.reverse!\n",
    "  f.rewind\n",
    "  f.write data\n",
    "end\n",
    "```\n",
    "\n",
    "× ○ 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 354,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# Dir.mkdir(\"/tmp/tmp2\", 0755) unless Dir.exist?(\"/tmp/tmp2\")\n",
    "# Dir.chdir(\"/tmp/tmp2\")\n",
    "# File.open(\"test.txt\",\"w\") do |io|\n",
    "#   io.write(\"Ruby Association\\n\")\n",
    "#   io.write(\"ルビーアソシエーション\\n\")\n",
    "#   io.write(\"るびー\\n\")\n",
    "#   io.write(\"Ruby on Rails\\n\")\n",
    "# end\n",
    "\n",
    "# open(\"/tmp/tmp2/test.txt\",\"__X__\") do |f|\n",
    "#   data = f.read.chomp\n",
    "#   data.reverse!\n",
    "#   f.rewind\n",
    "#   f.write data\n",
    "# end\n",
    "\n",
    "# puts %x(cat /tmp/tmp2/test.txt)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "aaby Association\n",
      "ルビーアソシエーション\n",
      "るびー\n",
      "Ruby on Rails\n",
      "\n"
     ]
    }
   ],
   "source": [
    "Dir.mkdir(\"/tmp/tmp2\", 0755) unless Dir.exist?(\"/tmp/tmp2\")\n",
    "Dir.chdir(\"/tmp/tmp2\")\n",
    "File.open(\"test.txt\",\"w\") do |io|\n",
    "  io.write(\"Ruby Association\\n\")\n",
    "  io.write(\"ルビーアソシエーション\\n\")\n",
    "  io.write(\"るびー\\n\")\n",
    "  io.write(\"Ruby on Rails\\n\")\n",
    "end\n",
    "\n",
    "open(\"/tmp/tmp2/test.txt\",\"r+\") do |f|\n",
    "  f.write(\"aa\")\n",
    "end\n",
    "\n",
    "puts %x(cat /tmp/tmp2/test.txt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題48. 以下の結果になるように__X__にコードを記述してください\n",
    "\n",
    "```\n",
    "puts File.__X__(\"/\", \"user\", \"bin\")\n",
    "\n",
    "/user/bin\n",
    "```\n",
    "\n",
    "〇 ○ 〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 359,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#puts File.__X__(\"/\", \"user\", \"bin\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題49. 以下の結果になるように__X__にコードを記述してください\n",
    "\n",
    "```\n",
    "t = Time.local(2000,1,1)\n",
    "\n",
    "__X__\n",
    "======\n",
    "200/01/01\n",
    "```\n",
    "\n",
    "1. printf((\"%Y/%m/%d\"), t.year, t.mon, t.day)\n",
    "1. printf((\"%Y/%m/%d\")) % t)\n",
    "1. printf(t.format(\"%Y/%m/%d\"))\n",
    "1. print(t.strftime(\"%Y/%m/%d\"))\n",
    "\n",
    "〇 ○　〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 116,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "#t = Time.local(2000,1,1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "deletable": true,
    "editable": true
   },
   "source": [
    "# 問題50. 以下のコードを実行するとどうなりますか\n",
    "\n",
    "〇 ○　〇"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 360,
   "metadata": {
    "collapsed": false,
    "deletable": true,
    "editable": true
   },
   "outputs": [],
   "source": [
    "# t1 = Time.gm(2010, 1, 1, 0, 0)\n",
    "# t2 = Time.gm(2010, 1, 1, 0, 1)\n",
    "# p t2 - t1"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Ruby 2.3.1",
   "language": "ruby",
   "name": "ruby"
  },
  "language_info": {
   "file_extension": ".rb",
   "mimetype": "application/x-ruby",
   "name": "ruby",
   "version": "2.3.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
